#include "lowlevel_proc.h"
#include "stream.h"
#include "wnds_mgmt.h"

using namespace std;


LRESULT CALLBACK LowLevelMouseProc(INT code, WPARAM wParam, LPARAM lParam)
{
    return LPARAM(0);

    if (code == HC_ACTION)
    {
        const char *msg;
        char msg_buff[128];
        switch (wParam)
        {
            case WM_LBUTTONDOWN: msg = "WM_LBUTTONDOWN"; break;
            case WM_LBUTTONUP:   msg = "WM_LBUTTONUP";   break;
            case WM_MOUSEMOVE:   msg = "WM_MOUSEMOVE";   return 1;
            case WM_MOUSEWHEEL:  msg = "WM_MOUSEWHEEL";  break;
            case WM_MOUSEHWHEEL: msg = "WM_MOUSEHWHEEL"; break;
            case WM_RBUTTONDOWN: msg = "WM_RBUTTONDOWN"; break;
            case WM_RBUTTONUP:   msg = "WM_RBUTTONUP";   break;
            default:
                sprintf_s(msg_buff, "Unknown msg: %u", wParam);
                msg = msg_buff;
                break;
        }
        switch (wParam)
        {
            case WM_LBUTTONDOWN: msg = "WM_LBUTTONDOWN"; break;
            default:
                sprintf_s(msg_buff, "Unknown msg: %u", wParam);
                msg = msg_buff;
                break;
        }

        stream::system_message("hook", "mouse: "+ string(msg));
    }

    return CallNextHookEx(NULL, code, wParam, lParam);
}

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{
    static const DWORD key_c = 0x43;

    if (nCode == HC_ACTION)
    {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
        DWORD Keystroke    = p->vkCode;
        switch (wParam)
        {
            case WM_LBUTTONDOWN:
            case WM_LBUTTONUP:
            case WM_MOUSEMOVE:
            case WM_MOUSEWHEEL:
            case WM_MOUSEHWHEEL:
            case WM_RBUTTONDOWN:
            case WM_RBUTTONUP:
            case WM_MBUTTONDOWN:
            case WM_MBUTTONUP:
                stream::system_message("hook", "mouse... "+ decToString(wParam) );
                break;
            case WM_KEYDOWN:
            {
                switch(Keystroke) {
                    case key_c:
                        if( isKeyDown(VK_CONTROL) || isKeyDown(VK_LCONTROL) || isKeyDown(VK_RCONTROL) ) {
                           stream::system_message("hook", "Exiting process...");

                           static UINT i = 0;
                           //cout<<endl<<"Close winds: "<<(closeWindows( window_info("MaskLayerClass1", "MaskLayer1") )==EXIT_SUCCESS)<<endl;

                           //cout<<endl<<"Mutex1: "<<processes::event_create(FALSE, NULL)<<endl;
                           //cout<<endl<<"Mutex2: "<<processes::event_create(FALSE,NULL)<<endl;
                           //cout<<endl<<"Mutex3: "<<processes::event_create(FALSE,NULL)<<endl;
                           //cout<<endl<<"Mutex4: "<<processes::event_create(FALSE,NULL)<<endl;
                           //cout<<endl<<"Mutex5: "<<processes::event_create(FALSE,NULL)<<endl;
                           if(i == 0)
                               stream::system_message("hook", "close winds: "+ decToString(closeWindows(window_info("MaskLayerClass1", "MaskLayer1"))) );
                            else if(i == 1)
                               stream::system_message("hook", "close winds: "+ decToString(closeWindows(window_info("MaskLayerClass1", "MaskLayer2"))) );
                            else if(i == 2)
                               stream::system_message("hook", "close winds: "+ decToString(closeWindows(window_info("MaskLayerClass1", "MaskLayer3"))) );
                            else
                               stream::system_message("hook", "close winds: "+ decToString(closeWindows(window_info("MaskLayerClass"))) );

                               i++;
                           test();
                           return 1;
                           //HWND maskWnd = FindWindow(L"MaskLayerClass" , L"MaskLayer");
                           //ExitProcess(EXIT_SUCCESS);
                        }

                        break;
                    default:
                    {}
                }
            }
            case WM_SYSKEYDOWN:
            {
                switch(Keystroke) {
                    case VK_F4:
                        stream::system_message("hook", "ALT+F4 blocked!");
                        //break;
                        return 1;
                    default:
                    {}
                }
            }
            case WM_KEYUP:
            case WM_SYSKEYUP:
            default:
            {}
        }
    }

    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

